package com.baway.empty

import IService
import android.app.Service
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import com.alibaba.android.arouter.facade.annotation.Route
import com.baway.base.path.RouterPath
import com.baway.base.view.BaseActivity
import com.baway.empty.databinding.ActivityEmptyBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = RouterPath.PATH_EMPTY_PAGE)
@AndroidEntryPoint
class EmptyActivity : BaseActivity<ActivityEmptyBinding>() {

    override fun getLayoutId(): Int = R.layout.activity_empty


    override fun initViews() {
        super.initViews()
        bindAidlService()
    }

    private var mAidlService: IService? = null

    /**
     * 定义服务链接对象
     */
    private val serviceConnect = object:ServiceConnection{

        /**
         * 服务已连接
         */
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            mAidlService = IService.Stub.asInterface(service)
            //调用方法
            mAidlService?.add(3, 5)
        }

        /**
         * 连服务已断开
         */
        override fun onServiceDisconnected(name: ComponentName) {
            mAidlService = null
        }


    }

    private fun bindAidlService(){
        val bindIntent = Intent().apply {
            setPackage("com.baway.park")              //一定是Service所在包名
            action = "com.baway.empty.AidlService"    //Service的类名
        }

        //绑定服务
        bindService(bindIntent, serviceConnect, Service.BIND_AUTO_CREATE)

    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        //解除绑定
        unbindService(serviceConnect)
    }



}